﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using Wangzeran.CodeMaker.Core.Util;

namespace Wangzeran.CodeMaker.Core.Model
{
    /// <summary>
    /// 模板文件
    /// </summary>
    public class TemplateFileModal
    {
        /// <summary>
        /// 匹配模板说明
        /// </summary>
        private const string PATTERN_TEMPLATE = @"<c:template.*?/>";

        private const string ATTR_NAME = "name";
        private const string ATTR_AUTHOR = "author";
        private const string ATTR_VERSION = "version";
        private const string ATTR_LANGUAGE = "language";
        private const string ATTR_OUTPUT = "output";

        public TemplateFileModal()
        {

        }

        /// <summary>
        /// 实例化模板文件实体
        /// </summary>
        /// <param name="relativePath">模板文件的相对路径</param>
        /// <param name="filePath">模板文件的绝对路径</param>
        public TemplateFileModal(string relativePath, string filePath)
        {
            try
            {
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException();
                }
                string template;
                using (TextReader reader = new StreamReader(filePath))
                {
                    template = reader.ReadToEnd();
                    reader.Close();
                }
                Match match = Regex.Match(template, PATTERN_TEMPLATE, RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    IDictionary<string, string> properties = StringUtil.ParseKeyValues(match.Value);
                    this.m_FilePath = relativePath;
                    ProcessProperties(properties);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 处理属性
        /// </summary>
        /// <param name="properties">属性的Key-Value集合</param>
        private void ProcessProperties(IDictionary<string, string> properties)
        {
            if (properties.ContainsKey(ATTR_NAME))
            {
                this.m_TemplateName = properties[ATTR_NAME];
            }
            if (properties.ContainsKey(ATTR_AUTHOR))
            {
                this.m_Author = properties[ATTR_AUTHOR];
            }
            if (properties.ContainsKey(ATTR_VERSION))
            {
                this.m_Version = properties[ATTR_VERSION];
            }
            if (properties.ContainsKey(ATTR_LANGUAGE))
            {
                this.language = properties[ATTR_LANGUAGE];
            }
            if (properties.ContainsKey(ATTR_OUTPUT))
            {
                this.output = properties[ATTR_OUTPUT];
            }
            this.isOk = true;
        }

        /// <summary>
        /// 模板名称
        /// </summary>
        private string m_TemplateName;
        /// <summary>
        /// 模板文件路径
        /// </summary>
        private string m_FilePath;
        /// <summary>
        /// 模板作者
        /// </summary>
        private string m_Author;
        /// <summary>
        /// 模板版本
        /// </summary>
        private string m_Version;
        /// <summary>
        /// 模板信息是否完整
        /// </summary>
        private bool isOk = false;
        /// <summary>
        /// 模板语言类型
        /// </summary>
        private string language;
        /// <summary>
        /// 文件输出类型
        /// </summary>
        private string output;
        


        /// <summary>
        /// 模板名称
        /// </summary>
        public string TemplateName
        {
            get { return m_TemplateName; }
            set { m_TemplateName = value; }
        }
        /// <summary>
        /// 模板文件路径
        /// </summary>
        public string FilePath
        {
            get { return m_FilePath; }
            set { m_FilePath = value; }
        }
        /// <summary>
        /// 模板作者
        /// </summary>
        public string Author
        {
            get { return m_Author; }
            set { m_Author = value; }
        }
        /// <summary>
        /// 模板版本
        /// </summary>
        public string Version
        {
            get { return m_Version; }
            set { m_Version = value; }
        }
        /// <summary>
        /// 模板信息是否完整
        /// </summary>
        public bool IsOk
        {
            get { return isOk; }
            set { isOk = value; }
        }
        /// <summary>
        /// 模板语言类型
        /// </summary>
        public string Language
        {
            get { return language; }
            set { language = value; }
        }
        /// <summary>
        /// 文件输出类型
        /// </summary>
        public string Output
        {
            get { return output; }
            set { output = value; }
        }

    }
}
